﻿#region File and License Information
/*
<File>
	<License>
		Copyright © 2009 - 2017, Daniel Vaughan. All rights reserved.
		This file is part of Calcium (http://calciumsdk.net), 
		which is released under the MIT License.
		See file /Documentation/License.txt for details.
	</License>
	<CreationDate>2010-12-09 21:03:24Z</CreationDate>
</File>
*/
#endregion

using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;

using Outcoder.Data;

#if WINDOWS_PHONE
using Microsoft.Phone.Tasks;
#endif

namespace Outcoder.LauncherModel.Launchers
{
	/* TODO: [DV] Comment. */
	public class MockPhotoChooserLauncher : IPhotoChooserLauncher
	{
		MockPhotoResult photoResult;

		public MockPhotoResult PhotoResult
		{
			get
			{
				return photoResult;
			}
			set
			{
				photoResult = value;
			}
		}

#if WINDOWS_UWP || NETFX_CORE
		public async Task<PhotoResultBase> ShowAsync()
		{
			Debug.WriteLine(string.Format("MockPhotoChooserTask shown PixelHeight {0}, PixelWidth {1}, ShowCamera {2}",
				PixelHeight, PixelWidth, ShowCamera));

			if (photoResult != null)
			{
				OnCompleted(photoResult);
				return new MockPhotoResult();
			}

			string assemblyShortName = typeof(MockPhotoChooserLauncher).GetTypeInfo().Assembly.FullName;
			assemblyShortName = assemblyShortName.Substring(0, assemblyShortName.IndexOf(','));
			var resourceUri = new Uri("ms-appx:///" + assemblyShortName + "/LauncherModel/Launchers/PhotoChooserLauncher/PhotoChooserLauncher.png", UriKind.Relative);

			Stream photoStream = await ApplicationResourceUtility.GetApplicationResourceStreamAsync(resourceUri);

			photoResult = new MockPhotoResult
			{
				OriginalFileName = "Calcium_PhotoChooserTask.png",
				/* TODO: Resize image according to PixelHeight and PixelWidth. */
				ChosenPhoto = photoStream,
			};
			photoResult.SetTaskResult(TaskResult.OK);
			OnCompleted(photoResult);

			return photoResult;
		}
#endif

		public async void Show()
		{
			Debug.WriteLine(string.Format("MockPhotoChooserTask shown PixelHeight {0}, PixelWidth {1}, ShowCamera {2}",
				PixelHeight, PixelWidth, ShowCamera));

			if (photoResult != null)
			{
				OnCompleted(photoResult);
				return;
			}

#if WINDOWS_UWP || NETFX_CORE
			string assemblyShortName = typeof(MockPhotoChooserLauncher).GetTypeInfo().Assembly.FullName;
			assemblyShortName = assemblyShortName.Substring(0, assemblyShortName.IndexOf(','));
			var resourceUri = new Uri("ms-appx:///" + assemblyShortName + "/LauncherModel/Launchers/PhotoChooserLauncher/PhotoChooserLauncher.png", UriKind.Relative);
#elif __ANDROID__
			var context = Android.App.Application.Context;
			var photoStream = context.Assets.Open("Launchers/PhotoChooserLauncher/PhotoChooserLauncher.png");
#else
			string assemblyShortName = Assembly.GetExecutingAssembly().FullName;
			assemblyShortName = assemblyShortName.Substring(0, assemblyShortName.IndexOf(','));
			var resourceUri = new Uri("/" + assemblyShortName + ";component/LauncherModel/Launchers/PhotoChooserLauncher/PhotoChooserLauncher.png", UriKind.Relative);
#endif

#if !__ANDROID__
			Stream photoStream = await ApplicationResourceUtility.GetApplicationResourceStreamAsync(resourceUri);
#endif
			photoResult = new MockPhotoResult
			{
				OriginalFileName = "Calcium_PhotoChooserTask.png",
				/* TODO: Resize image according to PixelHeight and PixelWidth. */
				ChosenPhoto = photoStream,
			};
			photoResult.SetTaskResult(TaskResult.OK);
			OnCompleted(photoResult);
		}

		public event EventHandler<PhotoResultBase> Completed;

		protected virtual void OnCompleted(PhotoResultBase e)
		{
			var tempHandler = Completed;
			if (tempHandler != null)
			{
				tempHandler(this, e);
			}
		}

		public int PixelHeight { get; set; }
		public int PixelWidth { get; set; }
		public bool ShowCamera { get; set; }

	}
}
